package games.ninJump.ui.effects
{
	import core.Global;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	
	import games.ninJump.NinJump;
	
	import interfaces.IUpdate;
	
	import module.Atom;
	
	import utils.MathTools;
	import utils.Particle;
	
	public class NinjaParticles implements IUpdate
	{
		private var particles:Array;
		private var m_start:Boolean;
		private var m_target:Sprite;
		private var m_follow:DisplayObject;
		
		private var m_bmd:BitmapData;
		private var m_state:int;
		
		public function NinjaParticles()
		{
			m_target= new Sprite();
			particles = new Array();
			m_bmd = Atom(Global.loadResource(NinJump.GAME_URL,"spark")).obj;
		}
		
		public function set start(b:Boolean):void
		{
			m_start = b;
		}
		
		public function get start():Boolean
		{
			return m_start;
		}
		
		public function update(e:Object = null):void
		{
			// make a new particle
			var spark:Bitmap = new Bitmap(m_bmd);
			var sp:Sprite = new Sprite();
			spark.x = - spark.width / 2 ;
			spark.y = - spark.height / 2;
			sp.addChild(spark);
			//
			
			var particle:Particle = new Particle(sp, m_target,m_follow.x,m_follow.y + 80 - sp.height / 2);
			
			// give it a random velocity 
			particle.setVel(MathTools.randRange(-2,+2),MathTools.randRange(3,5));
			
			// add drag;
			particle.drag = 0.96;
			
			// change size
			particle.setScale( Math.random());
			
			// init place
			
			
			//particle.shrink = .95 + .05 * Math.random();
			//particle.clip.scaleX = particle.clip.scaleY = Math.random() ;
			//gravity
			particle.gravity = .6;
			
			// fade out
			//particle.fade = 0.03;
			
			// rotate
			particle.directionRotate = true;
			//particle.clip.rotation = 360 * Math.random();
			
			// and add it to the particle array... 
			particles.push(particle);
			
			// if there are more than 40 particles delete the first one in the array... 
			while(particles.length > 15)
			{
				particle = Particle(particles.shift());
				particle.destroy();
			}
			
			// go through the array of particles ...
			for(var i:Number = 0;i<particles.length;i++)
			{
				// and update each one
				particles[i].update();
			}
		}
		
		public function get target():Sprite
		{
			return m_target;
		}
		
		public function set state(n:int):void
		{
			m_state = n;
			/*if(m_state == 1)
			{
				Global.addAction(this);
			}else
			{
				Global.deleteAction(this);
			}*/
		}
		
		public function get state():int
		{
			return m_state;
		}
		
		public function set follow(n:DisplayObject):void
		{
			m_follow = n;
		}
	}
}